<template>
  <div class="menu">
    <van-search placeholder="请输入搜索关键词" shape="round" />
    <div class="menu-box">
      <div class="menu-left">
        <van-sidebar v-model="activeKey">
          <van-sidebar-item :title="item.name" v-for="(item, index) in menuType" :key="index" @click="getProductByType(item.id)" />
        </van-sidebar>
      </div>
      <div class="menu-right">
        <!-- 商品 -->
        <div class="product-box">
          <div class="product-item" v-for="(item, index) in productData" :key="index" @click="viewDetail(item.id)">
            <Product :pro="item" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Product from '../components/Product.vue'
import {Toast} from "vant";
export default {
  name: 'Menu',
  data() {
    return {
      //激活的菜单类型下标
      activeKey: 0,
      //菜单类型数据
      menuType: [],
      //商品数据
      productData: []
    };
  },
  components:{
    Product
  },
  created() {
   this.getProductType();
  },
  methods: {
    //获取商品类型
    getProductType() {
      this.axios({
        method: 'get',
        url: '/teatype/all.do'
      }).then(result => {
        if (result.data.code === 200) {
          this.menuType = result.data.data;
          let type = result.data.data[0].id;
          this.getProductByType(type);
        }
      }).catch(err => {
      })
    },

    //根据商品类型获取商品数据
    getProductByType(type) {
      // if (type==null){
      //   type=1;
      // }
      this.axios.get("/teagoods/selByTid.do?tid="+type
        ).then(result => {
        if (result.data.code === 200) {
          this.productData = result.data.data;
        }

      }).catch(err => {
      })
    },

    //查看商品详情
    viewDetail(id) {
      this.$router.push({name: 'Detail', params: {id}});
    }
  }
}
</script>

<style lang="less" scoped>
.menu{
  position: fixed;
  top: 0;
  bottom: 50px;
  right: 0;
  left: 0;
  .product-box{
    display: flex;
    flex-wrap: wrap;
    .product-item{
      width: calc(50% - 5px);
      margin-right: 10px;
      margin-bottom: 10px;
      &:nth-child(2n){
        margin-right: 0;
      }
    }
  }
  .menu-box{
    display: flex;
    height: calc(100% - 50px);
    .menu-left{
      width: 100px;
      height: 100%;
      background-color: #F7F8FA;
      overflow-y: auto;
      /deep/ .van-sidebar{
        width: 100%;
      }
      /deep/ .van-sidebar-item--select::before{
        width: 2px;
        height: 60%;
        background-color: #0C34BA;
      }
    }
    .menu-right{
      width: calc(100% - 110px);
      height: 100%;
      overflow-y: auto;
      padding-left: 10px;
      padding-right: 10px;
      background-color: #fff;
    }
  }
}

</style>